package br.com.hsp.system.beans;

import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;

import br.com.hsp.organizacao.Consulta;
import br.com.hsp.organizacao.Instituicao;
import br.com.hsp.system.repository.ConsultaRepository;
import br.com.hsp.system.repository.GerenteInstituicaoRepository;
import br.com.hsp.system.repository.InstituicaoRepository;
import br.com.hsp.system.repository.MedicoClinicaRepository;
import br.com.hsp.system.repository.PacienteRepository;
import br.com.hsp.users.GerenteInstituicao;
import br.com.hsp.users.MedicoClinica;
import br.com.hsp.users.Paciente;


@ManagedBean
public class ConsultaBean {

	private Consulta consulta =  new Consulta();
	
	private Long pacienteId;
	
	private Long medicoId;
	
	private Long clinicaId;
	
	private List<Consulta> consultas;
		
	public void add () {
		EntityManager manager = this.getManager();
		ConsultaRepository consultaRepository = new ConsultaRepository(manager);
		PacienteRepository pacienteRepository = new PacienteRepository(manager);
		MedicoClinicaRepository medicoClinicaRepository = new MedicoClinicaRepository(manager);
		InstituicaoRepository instituicaoRepository= new InstituicaoRepository(manager);
		
		if(this.pacienteId != null) {
			Paciente paciente = pacienteRepository.procura(this.pacienteId);
			this.consulta.setPaciente(paciente);
		}
		if(this.medicoId != null) {
			MedicoClinica medico = medicoClinicaRepository.procura(medicoId);
			this.consulta.setMedico(medico);
		}
		if(this.clinicaId != null) {
			Instituicao instituicao = instituicaoRepository.procura(this.clinicaId);
			this.consulta.setClinica(instituicao);
		}
		if (this.consulta.getId() == null) {
			consultaRepository.add(this.consulta);
		} else {
			consultaRepository.atualiza(this.consulta);
		}
		
		this.consulta = new Consulta();
		this.pacienteId = null;
		this.medicoId = null;
		this.clinicaId =null;
	}
	
	public void preparaAlteracao() {
		Map<String, String> params = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap();
		Long id = Long.parseLong(params.get("id"));
		EntityManager manager = this.getManager();
		ConsultaRepository repository = new ConsultaRepository(manager);
		this.consulta = repository.procura(id);
		this.pacienteId = this.consulta.getPaciente().getId();
		this.medicoId = this.consulta.getMedico().getId();
		this.clinicaId =this.consulta.getClinica().getId();

	}
	
	
	public void remove() {
		Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		Long id = Long.parseLong(params.get("id"));
		EntityManager manager = this.getManager();
		ConsultaRepository repository = new ConsultaRepository(manager);
		repository.remove(id);
		this.consulta=null;
		this.consultas=null;
	}
	
	
	public List<Consulta> getConsultas() {
		if (this.consultas == null) {
			EntityManager manager = this.getManager();
			ConsultaRepository repository = new ConsultaRepository(manager);
			this.consultas = repository.getLista();
		}
		return this.consultas;
	}
	
	
	
	
	private EntityManager getManager() {
		FacesContext fc = FacesContext.getCurrentInstance();
		ExternalContext ec = fc.getExternalContext();
		HttpServletRequest request = (HttpServletRequest) ec.getRequest();
		return (EntityManager) request.getAttribute("EntityManager");

	}

	public Consulta getConsulta() {
		return consulta;
	}

	public void setConsulta(Consulta consulta) {
		this.consulta = consulta;
	}

	public Long getPacienteId() {
		return pacienteId;
	}

	public void setPacienteId(Long pacienteId) {
		this.pacienteId = pacienteId;
	}


	public Long getMedicoId() {
		return medicoId;
	}

	public void setMedicoId(Long medicoId) {
		this.medicoId = medicoId;
	}

	public Long getClinicaId() {
		return clinicaId;
	}

	public void setClinicaId(Long clinicaId) {
		this.clinicaId = clinicaId;
	}
	
	
}
